import 'dart:async';

import 'package:animated_bottom_navigation_bar/animated_bottom_navigation_bar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:hixing_app/const/color_res.dart';
import 'package:hixing_app/utils/logs.dart';
import 'package:hixing_app/views/community_index/community_index.dart';
import 'package:hixing_app/views/home_index/home_index.dart';
import 'package:hixing_app/views/main_page/ctrl/main_page_ctrl.dart';
import 'package:hixing_app/views/my_index/my_index.dart';
import 'package:hixing_app/widgets/toast/toast.dart';

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  MainPageCtrl _mainCtrl = Get.put(MainPageCtrl());
  final List<IconData> _icons = [
    Icons.home,
    Icons.social_distance,
    Icons.person,
  ];
  final List<IconData> _activeIcons = [
    Icons.home,
    Icons.social_distance,
    Icons.person,
  ];
  final List<String> _titles = [
    "首页",
    "社区",
    "我的",
  ];
  late final List<Widget> _pages = [
    const HomeIndex(),
    const CommunityIndex(),
    const MyIndex(),
  ];

  @override
  void initState() {
    super.initState();
    // TODO: implement initState
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  int last = 0;

  void doubleClickBack(back) {
    AppToast.showText(
      '再次点击退出',
      duration: const Duration(seconds: 3),
    );
    final now = DateTime.now().millisecondsSinceEpoch;
    if (now - last > 3000) {
      last = DateTime.now().millisecondsSinceEpoch;
      // return false;
    } else {
      last = 0;
      // return true;
    }
  }

  @override
  Widget build(BuildContext context) {
    logs('当前页面数量 :${_pages.length}');
    return PopScope(
      onPopInvoked: doubleClickBack,
      child: Scaffold(
        body: PageView.builder(
            physics: const NeverScrollableScrollPhysics(),
            controller: _mainCtrl.pageCtrl.value,
            itemBuilder: (ctx, index) {
              return _pages[index];
            }),
        // floatingActionButton: FloatingActionButton(
        //   backgroundColor: Theme.of(context).primaryColor,
        //   onPressed: () {
        //     _mainCtrl.changeIndex(MainTabEnum.my);
        //   },
        //   child: Icon(
        //     Icons.add,
        //   ),
        // ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        bottomNavigationBar: Obx(
          () => AnimatedBottomNavigationBar.builder(
            activeIndex: _mainCtrl.tabIndex.value,
            leftCornerRadius: 0,
            rightCornerRadius: 0,
            onTap: (index) {
              final MainTabEnum _page = MainTabEnum.values[index];
              if (_mainCtrl.tabIndex.value == index) return;

              logs('这是一个页面参数 $_page');
              _mainCtrl.changeIndex(_page);
            },
            scaleSize: 0.2,
            height: 60,

            gapWidth: 0,
            tabBuilder: (int index, bool isActive) {
              Color _color = isActive
                  ? Theme.of(context).primaryColor
                  : ColorRes.subTextColor;
              final _tabIcon = isActive ? _activeIcons[index] : _icons[index];
              return Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(_tabIcon, size: 22, color: _color),
                  const SizedBox(height: 4),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 8),
                    child: Text(
                      _titles[index],
                      maxLines: 1,
                      style: TextStyle(color: _color, fontSize: 11),
                    ),
                  )
                ],
              );
            },
            itemCount: _icons.length,
            //other params
          ),
        ),
      ),
    );
  }
}
